package com.innjia.sys.service.impl;

import com.innjia.base.page.Page;
import com.innjia.base.page.PageHelper;
import com.innjia.base.utils.Query;
import com.innjia.sys.dao.UserTenantDao;
import com.innjia.sys.entity.UserEntity;
import com.innjia.sys.entity.UserTenantEntity;
import com.innjia.sys.service.UserTenantService;
import com.innjia.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;


@Service("userTenantService")
public class UserTenantServiceImpl extends UserServiceImpl implements UserTenantService {
    @Autowired
    private UserTenantDao userTenantDao;

    @Override
    public UserTenantEntity queryObject(String id) {
        return userTenantDao.queryObject(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(UserEntity user) {
        UserTenantEntity userTenant = (UserTenantEntity) user;
        super.save(userTenant);
        userTenant.setUserId(userTenant.getId());
        userTenantDao.save(userTenant);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(UserEntity user) {
        UserTenantEntity userTenant = (UserTenantEntity) user;
        super.update(userTenant);
        userTenantDao.update(userTenant);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(String id) {
        super.delete(id);
        userTenantDao.delete(id);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteBatch(String[] ids) {
        super.deleteBatch(ids);
        userTenantDao.deleteBatch(ids);
    }

    @Override
    public UserEntity queryByUid(String uid, String loginSrc) {
        UserTenantEntity entity = new UserTenantEntity();
        UserUtils.setUid(entity, uid, loginSrc);
        return userTenantDao.queryByUid(entity);
    }

    @Override
    public UserEntity queryByPhone(String phone) {
        return userTenantDao.queryByPhone(phone);
    }

    @Override
    public Page queryList(Map<String, Object> map) {
        Query query = (Query) map;
        PageHelper.startPage(query.getPage(), query.getLimit());
        userTenantDao.queryList(map);
        return PageHelper.endPage();
    }

    @Override
    public UserEntity queryByLoginName(String logName) {
        return userTenantDao.queryByLoginName(logName);
    }
}
